home *** CD-ROM | disk | FTP | other *** search
- /**************************************************************************/
- /* Calculate π by dropping a pin on a wood plank floor. */
- /* If any part of the pin touches the "crack" between boards, */
- /* it is a HIT. */
- /* π is calculated from the ratio of hits to total drops of the pin. */
- /* */
- /* M\Cooper */
- /* 3425 Chestnut Ridge Rd. */
- /* Grantsville, MD 21536 */
- /* ------------------------- */
- /* email: thegrendel@aol.com */
- /* */
- /* 06/91 */
- /* Source code placed in the public domain. */
- /**************************************************************************/
-
-
-
-
- #include <math.h>
- #include <stdlib.h>
- #include <conio.h>
- #include <stdio.h>
-
- #define MAX 1000000
- #define MAX_RND 32765
- #define MULTIPLIER 10.0
-
- double getrand (int );
-
- /**********************************************************************/
- /* GETRAND(n) */
- /* Outputs random number (0.0 - n) */
- /**********************************************************************/
-
- double getrand( int upper_lim )
-
- {
- double scale_factor, /*scaling factor*/
- rndnum,
- rand0_1, /*random number between 0.0 and 1.0*/
- random_number;
-
- scale_factor = (double)MAX_RND;
- rndnum = (double)random( MAX_RND );
-
- rand0_1 = rndnum / scale_factor;
- random_number = rand0_1 * (double)upper_lim;
-
- return( random_number );
- }
-
- /**************************************************************************/
-
-
- void main()
- {
- double Dx,
- theta,
- ratio,
- Pi;
- long hits = 0,
- drops = 0;
-
- randomize();
- clrscr();
-
-
- while (drops < MAX)
- {
- drops++;
- Dx = getrand( 1 ); /*Generate horizontal position of one end of the pin*/
- theta = getrand( 180 ); /* Angle of the pin from the horizontal */
-
- if( theta <= 90.0 )
- if( Dx + cos( theta ) >= 1.0 )
- hits++;
-
- if ( theta > 90.0 )
- if( ( Dx - cos( 180.0 - theta ) ) <= 0.0 )
- hits++;
-
- ratio = (double)hits / (double) drops;
-
- printf( "Drops = %5ld --- Hits = %5ld ============> π ≈ %f \n",
- drops, hits, MULTIPLIER * ratio);
- }
-
- }
-